<%--
  Created by IntelliJ IDEA.
  User: 邓浩阳
  Date: 2018/4/13
  Time: 20:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jspf" %>
<html>
<head>
    <title>管理员账户列表</title>
    <script>
        function load(param,sign) {
            $.ajax({
                url: "${hkxyPath}/user/admin-list-json",
                type: "post",
                data: param,
                dataType: "json",
                success: function (result) {
                    $("#loadcartoon").hide();
                    if(result.length<10){
                        $("#loadovertip").show();
                        $("#loadmoreBtn").hide();
                    };
                    var htmlStr = '';
                    $.each(result, function (i, val) {
                        if (val.realname == '' || val.realname == null) {
                            val.realname = "<span class='text-danger'>待完善</span>";
                        }
                        var p;
                        if (val.power == '' || val.power == null) {
                            p = "<span class='text-danger'>暂无权限</span>";
                        } else if (val.power == 'all') {
                            p = "所有权限";
                        } else {
                            p = val.power.replace("1", "新闻动态")
                                .replace("2", "通知公告")
                                .replace("3", "教务信息")
                                .replace("4", "学生工作")
                                .replace("5", "招生信息")
                                .replace("6", "就业信息")
                                .replace("7", "国际交流")
                                .replace("8", "科研工作")
                                .replace("9", "党群工作")
                            ;
                        }
                        htmlStr += "<tr id=tr"+val.id+">" +
                            "<td>" + val.username + "</td>" +
                            "<td>" + val.realname + "</td>" +
                            "<td>" + p + "</td>" +
                            "<td><button class='btn btn-info btn-sm' data-toggle='modal' data-target='#modifyAdminModal' onclick='modifyAdminBtn(" + val.id + ")'>编辑</button> <button class='btn btn-default btn-sm' onclick='deleteAdmin(" + val.id + ")'>删除</button></td>" +
                            "</tr>";
                    });
                    if(sign==1){
                        $("#userList").html(htmlStr);
                    }else {
                        $("#userList").append(htmlStr);
                    }
                },
                error: function (data) {
                    $("#loadcartoon").hide();
                }
            });
        }

        var param = {key: '', pageNum: 1};
        $(function () {
            $("#loadcartoon").show();
            param.pageNum = 1;
            load(param,1);
        });

        function search() {
            var keyword = $("#keyword").val();
            if (keyword == '') {
                param.key = '';
                param.pageNum=1;
                load(param,1);
            } else {
                param.key = keyword;
                param.pageNum=1;
                load(param,1);
            }
        }

        function addAdmin() {
            param.pageNum=1;
            $.ajax({
                url: "${hkxyPath}/user/admin-add",
                type: "post",
                data: $("#addAdminForm").serialize(),
                dataType: "json",
                success: function (result) {
                    if (result.code == 1) {
                        tips(1);
                        load(param,1);
                    } else {
                        tips(0);
                    }
                },
                error: function () {
                    tips(0);
                }
            });
        }

        function modifyAdminBtn(id) {
            //    $("#modifyAdminModal input[name='powers']").attr("checked",false);
            $.post("${hkxyPath}/user/admin/" + id, function (result) {
                $("#username").val(result.username);
                $("#realname").val(result.realname);
                $("#sex").val(result.sex);
                $("#address").val(result.address);
                $("#phone").val(result.phone);
                $("#email").val(result.email);
                $("#subModifyBtn").val(result.id);
                $("#birthday").val(dateFormat(result.birthday));
                var power = result.power;
                if (power != null) {
                    var checkBoxArray = power.split(",");
                    for (var i = 0; i < checkBoxArray.length; i++) {
                        $("#modifyAdminModal input[name='powers']").each(function () {
                            if ($(this).val() == checkBoxArray[i]) {
                                $(this).attr("checked", true);
                            }
                        })
                    }
                }
            })
        }

        function subModifyAdmin(id) {
            param.pageNum=1;
            var powers = $("#modifyAdminModal input[name='powers']:checked");
            var checkBoxValue = "";
            powers.each(function () {
                checkBoxValue += $(this).val() + ",";
            })
            var power = checkBoxValue.substring(0, checkBoxValue.length - 1);
            var data = {id: id, power: power};
            $.post("${hkxyPath}/user/admin-modify", data, function (result) {
                if (result.code == 1) {
                    tips(1);
                    load(param,1);
                } else {
                    tips(0);
                }
            })
        }

        function deleteAdmin(id) {
            var trid="#tr"+id;
            var c = confirm("删除后不可恢复！！！\n确认删除？？？");
            if (c) {
                $(trid).remove();
                $.post("${hkxyPath}/user/admin-delete", {id: id}, function (result) {
                    tips(1);
                })
            }
        }
        function adminloadmore() {
            $("#loadcartoon").show();
            param.pageNum+=1;
            load(param,0);
        }

        function tips(i) {
            if (1 == i) {
                $("#success").show();
                setTimeout(function () {
                    $("#success").hide();
                }, 1000);
            } else {
                $('#error').show();
                setTimeout(function () {
                    $("#success").hide();
                }, 2000);
            }
        }

        function closeModel() {
        }
    </script>
</head>
<body>
<div>
    <div id="success" class="alert alert-success" hidden>成功！正常完成了操作。！</div>
    <div id="error" class="alert alert-danger alert-dismissable" hidden>
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        错误！请检查网络连接或修改数据（用户名不可重复）。
    </div>
    <!-- Head -->
    <div class="header-bottom-my1">
        <h2>管理员账户</h2>
    </div>
    <!-- Filter -->
    <div class="padding-5">
        <button class='btn btn-primary pull-left' data-toggle="modal" data-target="#addAdminModal">添加用户</button>
        <div class="input-group col-md-offset-8 col-md-4">
            <input id="keyword" type="text" class="form-control" placeholder="查找用户">
            <span class="input-group-btn">
                <button class="btn btn-default" type="button" style="height: 34px;" onclick="search()">
                    <i class="fa fa-search"></i>
                </button>
            </span>
        </div>
    </div>
    <!-- List -->
    <div class="padding-5 well" style="background-color:white;">
        <table class="table table-hover" style="padding-top: 10px;padding-bottom: 10px">
            <thead>
            <tr>
                <th class="col-md-2">用户名</th>
                <th class="col-md-2">真实姓名</th>
                <th class="col-md-6">拥有权限</th>
                <th class="col-md-2">操作</th>
            </tr>
            </thead>
            <tbody id="userList">

            </tbody>
        </table>
    </div>
    <div>
        <div id="loadcartoon" hidden>
            <p class="text-center"><img src="${imgPath}/loading.gif">正在加载</p>
        </div>
        <div id="loadovertip" hidden>
            <p class="text-center">没有更多数据了</p>
        </div>
        <div id="loadmoreBtn" class="loadmore" onclick="adminloadmore()">
            点击加载更多
        </div>
    </div>
    <!-- 添加用户的模态框（Modal） -->
    <div class="modal fade" id="addAdminModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="closeModel()">
                        &times;
                    </button>
                    <h4 class="modal-title" id="modalLabel">添加管理员帐户</h4>
                </div>
                <div class="modal-body" id="addAdminModelBody">
                    <form role="form" id="addAdminForm">
                        <div class="form-group inline-form">
                            <label for="username">用户名：</label>
                            <input type="text" class="form-control" name="username" placeholder="分配用户名">
                        </div>
                        <div class="form-group inline-form">
                            <label for="password">密　码：</label>
                            <input type="password" class="form-control" name="password" placeholder="默认密码123456"
                                   value="123456">
                            <p class="help-block">密码可以为空，默认为123456</p>
                        </div>
                        <label for="name">权限选择：</label>
                        <div class="checkbox">
                            <label><input type="checkbox" name="powers" value="1">新闻动态</label>
                            <label><input type="checkbox" name="powers" value="2">通知公告</label>
                            <label><input type="checkbox" name="powers" value="3">教务信息</label>
                            <label><input type="checkbox" name="powers" value="4">学生工作</label>
                            <label><input type="checkbox" name="powers" value="5">招生信息</label>
                            <label><input type="checkbox" name="powers" value="6">就业信息</label>
                            <label><input type="checkbox" name="powers" value="7">国际交流</label>
                            <label><input type="checkbox" name="powers" value="8">科研工作</label>
                            <label><input type="checkbox" name="powers" value="9">党群工作</label>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="close"
                            onclick="closeModel()">关闭
                    </button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="addAdmin()" id="subBtn">
                        提交
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <!-- 修改用户的模态框（Modal） -->
    <div class="modal fade" id="modifyAdminModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="closeModel()">
                        &times;
                    </button>
                    <h4 class="modal-title">修改管理员权限</h4>
                </div>
                <div class="modal-body">
                    <p class="help-block">以下信息进权限可以修改，其他资料未经本人同意不可修改！</p>
                    <form role="form" id="modifyAdminForm">
                        <fieldset disabled>
                            <div class="form-group inline-form">
                                <label for="username">用户名：</label>
                                <input type="text" class="form-control" id="username" name="username">
                            </div>
                            <div class="form-group inline-form">
                                <label for="username">真实姓名：</label>
                                <input type="text" class="form-control" name="realname" id="realname">
                            </div>
                            <div class="form-group inline-form">
                                <label for="username">性别：</label>
                                <input type="text" class="form-control" name="sex" id="sex">
                            </div>
                            <div class="form-group inline-form">
                                <label for="username">生日：</label>
                                <input type="text" class="form-control" name="birthday" id="birthday">
                            </div>
                            <div class="form-group inline-form">
                                <label for="username">地址：</label>
                                <input type="text" class="form-control" name="address" id="address">
                            </div>
                            <div class="form-group inline-form">
                                <label for="username">手机：</label>
                                <input type="text" class="form-control" name="phone" id="phone">
                            </div>
                            <div class="form-group inline-form">
                                <label for="username">邮箱：</label>
                                <input type="text" class="form-control" name="email" id="email">
                            </div>
                        </fieldset>

                        <label for="name">权限选择：</label>
                        <div class="checkbox">
                            <table style="width: 100%">
                                <tr>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="1">新闻动态</label>
                                    </td>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="2">通知公告</label>
                                    </td>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="3">教务信息</label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="4">学生工作</label>
                                    </td>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="5">招生信息</label>
                                    </td>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="6">就业信息</label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="7">国际交流</label>
                                    </td>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="8">科研工作</label>
                                    </td>
                                    <td>
                                        <label><input type="checkbox" name="powers" value="9">党群工作</label>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" onclick="closeModel()">关闭
                    </button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal" id="subModifyBtn" value=""
                            onclick="subModifyAdmin(this.value)">提交
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

</div>
</body>
</html>
